package org.quarenden.demo.sequence;

import org.quarenden.demo.dao.SequenceRepository;
import org.quarenden.demo.util.SpringUtils;

public class JdbcSequenceIdProvider implements SequenceIdProvider {

    //@Autowired
    private SequenceRepository sequenceRepository = SpringUtils.getBean("sequenceRepository");

    @Override
    public SequenceId create(String name) {
        return new SequenceId(this, name, 1);
    }

    @Override
    public SequenceId create(String name, Integer begin) {
        return new SequenceId(this, name, begin);
    }

    @Override
    public Integer load(String name) {
        return sequenceRepository.findOne(name).getNextVal();
    }

    @Override
    public void store(Integer value,String name) {
        sequenceRepository.updateByName(value,name);
    }
}
